package com.demo.controller;

import com.demo.common.BaseRes;
import com.demo.dto.LoginRequestDTO;
import com.demo.service.LoginUserService;
import com.demo.util.Md5Utils;
import com.demo.pojo.loginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author leiliang
 * @version 1.0
 * @Description
 * @date 2020/12/14 14:51
 */

@Controller
@Slf4j
@RequestMapping(value = "/api")
@Api(tags = {"后台登录接口"}, produces = "application/json")
public class LoginController {

    @Autowired
    LoginUserService loginUserService;

    @RequestMapping("/index")
    public String index(){
        return "login";
    }

    @ApiOperation(value = "后台登录接口", httpMethod = "POST", notes = "后台登录接口")
    @PostMapping("/login")
    @ResponseBody
    public BaseRes login(HttpServletRequest request){
        String username = request.getParameter("username");
        String passsword = request.getParameter("password");
        String pass = Md5Utils.string2Md5(passsword);

        loginUser login = new loginUser();
        login.setUsername(username);
        login.setPassword(pass);
        login.setCreateTime(new Date());
        login.setLoginTime(new Date());
        BaseRes baseRes = loginUserService.login(login);
        if (baseRes != null){
            if ("-1".equals(baseRes.getCode())){
                return baseRes;
            }
        }
        return baseRes;
    }
}
